﻿using System;
using System.Linq;
using System.Web.Mvc;
using HawaiRestaurant.Models.DataContext;
using HawaiRestaurant.Models.Repositories;

namespace HawaiRestaurant.Controllers
{
    public class HomeController : Controller
    {
        #region Private Methods
        private readonly IRepository _iRepository;
        #endregion

        #region Constructor
        public HomeController()
            : this(new Repository())
        {
        }
        public HomeController(IRepository repository)
        {
            _iRepository = repository;
        }
        #endregion

        #region "Public Method"
        public ViewResult Index()
        {
            return View();
        }

        public ViewResult HomePageIndex()
        {
            return View();
        }

        [HttpPost]
        public JsonResult GetAllCategories()
        {
            return Json(_iRepository.GetCategory());
        }

        [HttpPost]
        public JsonResult GetNewItem(int n)
        {
            return Json(_iRepository.GetNew(n));
        }

        [HttpPost]
        public JsonResult GetFavoritesItem(int n)
        {
            return Json(_iRepository.GetFavorite(n));
        }

        [HttpPost]
        public JsonResult GetFoodDetailsById(int id)
        {
            return Json(_iRepository.GetFoodDetails(id));
        }

        [HttpPost]
        public JsonResult GetDrinkDetailsById(int id)
        {
            return Json(_iRepository.GetDrinkDetails(id));
        }

        [HttpPost]
        public JsonResult GetMoreData(int number, int current, string cat)
        {
            var a = _iRepository.GetNew(10000).Skip((current - 1) * number).Take(number);
            return Json(cat == "All" ? _iRepository.GetNew(10000).Skip((current - 1) * number).Take(number) : _iRepository.GetNew(10000).Where(c => c.ItemTypeName == cat).Skip((current - 1) * number).Take(number));
        }

        [HttpPost]
        public JsonResult CheckLogin(string user, string pass)
        {
            var result = _iRepository.Login(new User
                                                {
                                                    UserName = user,
                                                    Password = pass,
                                                    IsActive = true
                                                });
            return Json(result);
        }

        [HttpPost]
        public JsonResult SearchByItemName(string name)
        {
            return Json(_iRepository.GetNew(10000).Where(n => n.Name.ToLower().Contains(name.ToLower())));
        }

        [HttpPost]
        public JsonResult InsertContract(string obj)
        {
            try
            {
                var temp = obj.Split('@');
                var contract = new Contract
                                   {
                                       StaffId = int.Parse(temp[0]),
                                       CustomerId = int.Parse(temp[1]),
                                       ContractDate = DateTime.UtcNow,
                                       PartyDate = DateTime.Parse(temp[3]),
                                       PartyAddress = temp[4],
                                       PartyType = temp[5],
                                       Promotion = temp[6],
                                       PartyConsultancy = temp[7],
                                       PartySetupStyle = temp[8],
                                       PaymentMethod = temp[9],
                                       TransportationVehicle = temp[10],
                                       Discount = int.Parse(temp[11]),
                                       OlderAmount = int.Parse(temp[12]),
                                       ChildAmount = int.Parse(temp[13]),
                                       CustomerAmountPerTable = int.Parse(temp[14]),
                                       OtherRequirements = temp[15],
                                       ContractTotalAmount = int.Parse(temp[16]),
                                       TotalDeposit = 0
                                   };
                return Json(_iRepository.CreateContract(contract));
            }
            catch (Exception)
            {
                return Json(-1);
            }
        }

        [HttpPost]
        public JsonResult GetOrderParty(int contractId)
        {
            return Json(_iRepository.GetOrderParty(contractId));
        }

        [HttpPost]
        public JsonResult InsertToContractFood(string obj)
        {
            var food = obj.Split('@');
            var f = new Contract_Food
                        {
                            ContractId = int.Parse(food[0]),
                            FoodId = int.Parse(food[1]),
                            Quantity = int.Parse(food[2]),
                            Price = decimal.Parse(food[3])
                        };
            return Json(_iRepository.InsertToContractFood(f));
        }

        [HttpPost]
        public JsonResult InsertToContractDrink(string obj)
        {
            var drink = obj.Split('@');
            var d = new Contract_Drink
            {
                ContractId = int.Parse(drink[0]),
                DrinkId = int.Parse(drink[1]),
                Quantity = int.Parse(drink[2]),
                Price = decimal.Parse(drink[3])
            };
            return Json(_iRepository.InsertToContractDrink(d));
        }
        #endregion
    }
}
